import EaseInOut, {EaseInOutOption} from '@/libs/easeInOut'

// /**
//  * 匀减速效果
//  * @param param0
//  * @returns
//  */
// const useEaseInOut = ({
//   isFormat = false,
//   autoplay = true,
//   useEasing = true,
//   startVal = 0,
//   endVal = 0,
//   duration = 3000,
//   decimals = 0,
//   decimal = '.',
//   separator = ',',
//   prefix = '',
//   suffix = '',
//   easingFn = (t: number, b: number, c: number, d: number) => {
//     return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
//   },
//   callback = () => { },
//   mountedCallback = () => { },
// }) => {
//   let state = reactive<any>({
//     localStartVal: 0,
//     displayValue: 0,
//     printVal: null,
//     paused: false,
//     localDuration: 3000,
//     startTime: null,
//     timestamp: null,
//     remaining: null,
//     rAF: null
//   })
//   const countDown = computed(() => startVal > endVal)
//   const start = (localStartVal?: number) => {
//     state.localStartVal = localStartVal || startVal;
//     state.startTime = null;
//     state.localDuration = duration;
//     state.paused = false;
//     state.rAF = requestAnimationFrame(count);
//   }
//   const count = (timestamp: any) => {
//     if (!state.startTime) state.startTime = timestamp;
//     state.timestamp = timestamp;
//     const progress = timestamp - state.startTime;
//     state.remaining = state.localDuration - progress;
//     if (useEasing) {
//       if (countDown.value) {
//         state.printVal = state.localStartVal - easingFn(progress, 0, state.localStartVal - endVal, state.localDuration)
//       } else {
//         state.printVal = easingFn(progress, state.localStartVal, endVal - state.localStartVal, state.localDuration);
//       }
//     } else {
//       if (countDown.value) {
//         state.printVal = state.localStartVal - ((state.localStartVal - endVal) * (progress / state.localDuration));
//       } else {
//         state.printVal = state.localStartVal + (endVal - state.localStartVal) * (progress / state.localDuration);
//       }
//     }
//     if (countDown.value) {
//       state.printVal = state.printVal < endVal ? endVal : state.printVal;
//     } else {
//       state.printVal = state.printVal > endVal ? endVal : state.printVal;
//     }
//     state.displayValue = formatNumber(state.printVal)
//     if (progress < state.localDuration) {
//       state.rAF = requestAnimationFrame(count);
//     } else {
//       callback()
//     }
//   }
//   const isNumber = (val: any) => {
//     return !isNaN(parseFloat(val))
//   }
//   function formatNumber(num: any) {
//     num = num.toFixed(decimals);
//     if (!isFormat) return num
//     num += '';
//     const x = num.split('.');
//     let x1 = x[0];
//     const x2 = x.length > 1 ? decimal + x[1] : '';
//     const rgx = /(\d+)(\d{3})/;
//     if (separator && !isNumber(separator)) {
//       while (rgx.test(x1)) {
//         x1 = x1.replace(rgx, '$1' + separator + '$2');
//       }
//     }
//     return prefix + x1 + x2 + suffix;
//   }
//   onMounted(() => {
//     if (autoplay) {
//       start();
//     }
//     mountedCallback()
//   })
//   onUnmounted(() => {
//     cancelAnimationFrame(state.rAF)
//   })
//   return { count: toRef(state, 'displayValue'), countStart: start }
// }
// export default useEaseInOut

/**
 * 数字匀减速效果
 * @param param0
 * @returns
 */
const useEaseInOut = ({
  isFormat = false,
  autoplay = true,
  useEasing = true,
  startVal = 0,
  endVal = 0,
  duration = 3000,
  decimals = 0,
  decimal = '.',
  separator = '',
  prefix = '',
  suffix = ''
}: EaseInOutOption) => {
  const state = reactive<any>({
    displayValue: 0
  })

  const easeInOut = new EaseInOut({
    startVal,
    endVal,
    isFormat,
    useEasing,
    duration,
    decimal,
    decimals,
    separator,
    prefix,
    suffix,
    running: val => {
      state.displayValue = val
    }
  })

  if (endVal && autoplay) {
    startEaseInout({startVal, endVal})
  }

  function startEaseInout(data: EaseInOutOption) {
    easeInOut.init(Object.assign(data, {autoplay: true}))
  }

  onUnmounted(() => {
    easeInOut.stop()
  })

  return {
    count: toRef(state, 'displayValue'),
    countStart: startEaseInout
  }
}

export default useEaseInOut
